// JavaScript Document

//function to select,deslect message
function sel_message(msgName,len,folder)
{
	//window.alert(document.messages.mess_0.checked);
	//formEle	= $F($A('msgForm'));
	
	var allNodes	= Array();
	//alert(folder);
	//alert(msgName);
	allNodes = document.getElementById('msgForm');
	//alert($A($('msgForm')));
			$('message_action_option').innerHTML	= '<select name="message_action" id="message_action" onchange="javascript:messageProcess(\''+folder+'\',\''+baseUrl+'\')" ><option value="select_action">--</option><option value="mark_unread">Позначити як нові</option><option value="mark_read">Позначити як прочитані</option><option value="move_to_trash">Видалити відмічені</option></select>';
	if(msgName=='all' || msgName=='none')
	{
			selectAllNone(msgName,len);
	}
	else
	{
		if(msgName=='read' && folder=='inbox')
			{
				msgName	= 'mess_i_r_';
				unCheck	= 'mess_i_u_';
			}
		else if(msgName=='read' && folder=='sent')
			{
				msgName	= 'mess_s_r_';
				unCheck	= 'mess_s_u_';
			}
		else if(msgName=='unread' && folder=='inbox')
			{
				msgName	= 'mess_i_u_';
				unCheck	= 'mess_i_r_';
			}
		else if(msgName=='unread' && folder=='sent')
			{
				msgName	= 'mess_s_u_';
				unCheck	= 'mess_s_r_';
			}
		
		
		for(i = 0; i < allNodes.length; i++) 
		{
			//do something to each form field
			for (j = 1; j <= len; j++)
				{
					if(allNodes[i].name	== unCheck+j)
						$(unCheck+j).checked = false;
					else if(allNodes[i].name	== msgName+j)
					{
						if($(msgName+j).checked)
							$(msgName+j).checked = false;
						else
							$(msgName+j).checked = true;
						selected=true;
					}
				}
		}
		if(!selected)
		{
			selectAllNone('none',len);
			$('message_menu').style.display	= 'none';
		}
		if(msgName=='mess_i_u_' || msgName=='mess_s_u_')
			$('message_action_option').innerHTML	= '<select name="message_action" id="message_action" onchange="javascript:messageProcess(\''+folder+'\',\''+baseUrl+'\')" ><option value="select_action">--</option><option value="mark_read">Позначити як прочитані</option><option value="move_to_trash">Видалити відмічені</option></select>';
		else
			$('message_action_option').innerHTML	= '<select name="message_action" id="message_action" onchange="javascript:messageProcess(\''+folder+'\',\''+baseUrl+'\')" ><option value="select_action">--</option><option value="mark_unread">Позначити як нові</option><option value="move_to_trash">Видалити відмічені</option></select>';
	}

}
status=false;
function selectAllNone(msgName,len)
{
	//alert(msgName);
	var allNodes = document.getElementById('msgForm');

	if(msgName=='all' || msgName=='none')
	{
		
		if(msgName=='all')
			{
				status	= true;
			}
		else
			{
				status	= false;
			}
		for(i = 0; i < allNodes.length; i++) 
		{
			//do something to each form field
			//alert(allNodes[i]);
			for (j = 1; j <= len; j++)
			{
				var msg1='mess_i_r_' + j;
				if(allNodes[i].name	==msg1)
					$(msg1).checked	= status;
				var msg2='mess_i_u_' + j;
				if(allNodes[i].name	==msg2)
					$(msg2).checked	= status;
				var msg3='mess_s_r_' + j;
				if(allNodes[i].name	==msg3)
					$(msg3).checked	= status;
				var msg4='mess_s_u_' + j;
				if(allNodes[i].name	==msg4)
					$(msg4).checked	= status;
				var checkall='checkall';
				$(checkall).checked	= status;
			}
		}
	}
}

function checkedMsg (folder) {

	 if (status == false)
          {
           status = true;
           $('message_action_option').innerHTML	= '<select name="message_action" id="message_action" onchange="javascript:messageProcess(\''+folder+'\',\''+baseUrl+'\')" ><option value="select_action">--</option><option value="mark_unread">Позначити як нові</option><option value="mark_read">Позначити як прочитані</option><option value="move_to_trash">Видалити відмічені</option></select>';
          }

      }


//to show the div content
function show_div(div_id)
{
	//alert(div_id);
	$(div_id).style.display	= 'block';
}
//to close div
function close_div(div_id)
{
	$(div_id).style.display	= 'none';
}


//function to chech the text limit of the given field and enable error messaga
//arg1	: field id
//arg2	: text limit
//arg3	: div id or id in which the error msg defined
function textLimit(field_id,limit,div_id)
{
	var field_content	= $(field_id).value;
	if(field_content.length>limit)
		{
			$(field_id).value		= field_content.substring(0,limit);
			$(div_id).style.display	= 'block';
		}
	else if(field_content.length<=limit)
		$(div_id).style.display	= 'none';
}


//function add another school
function add_school()
{
	var section_value	= $('school_section_count').value;
	section_value	= parseInt(section_value)+1;
	$('school_section_count').value	= section_value;
	if(section_value<5)
		{
			$('education_'+section_value).style.display='block';
			if(section_value==4)
				$('add_link').style.display='none';
		}
}

//function to enable/disable end work span
function toggleEndWorkSpan(span_name)
{
	res	= $(span_name+'current').checked;
	if(res)
		{
			$(span_name+'endspan').style.display	= 'none';
			$(span_name+'present').style.display	= 'block';
		}
	else
		{
			$(span_name+'endspan').style.display	= 'block';
			$(span_name+'present').style.display	= 'none';
		}
		
}

//function add another job
function add_job()
{
	var section_value	= $('work_section_count').value;
	section_value	= parseInt(section_value)+1;
	$('work_section_count').value	= section_value;
	if(section_value<5)
		{
			$('work_history_'+section_value).style.display='block';
			if(section_value==4)
				$('add_link').style.display='none';
		}
}



//cehck valid email
function isEmail(str)
{
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var objRegExp = /(^[a-z]([a-z0-9_\.]*)@([^-][a-z0-9-_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z0-9_\.]*)@([^-][a-z0-9-_\.]*)(\.[a-z]{2,3})(\.[a-z]{2})*$)/i;
	return (!r1.test(str) && objRegExp.test(str));
}



//function to show div block with content
function showDivContent(divId,divContent)
{
	$(divId).style.display		= 'block';
	$(divId).innnerHTML			= divContent;
}
function browseEvents()
{
	$('event_category').value	= $('event_category_select').value;
	$('event_date').value		= $('event_date_select').value;
	$('browse_event_form').submit();
}
//
function showSchoolCity()
{

			$('school_city_default_div').style.display	= 'block';
		
}
//function to validate work form
function validateWork()
{	
	var totalJobs = $('work_section_count').value;
	var company	= $('work_history_1_company').value;
	if(company=='')
	{	
			$('work_history_result').innerHTML	= '<div class="msg-error">Помилка: Не вказаний роботодавець</div>';
			$('work_success').style.display	= 'none';
			$('work_error').style.display	= 'none';
	}
	else if(totalJobs!=0)
	{
		for (i = 1; i <= totalJobs; i++) 
		{
			if($('work_history_'+i+'_start_year_select').value==$('work_history_'+i+'_end_year_select').value)
			{
				if($('work_history_'+i+'_start_month_select').value>=$('work_history_'+i+'_end_month_select').value)
				{
					$('work_history_result').innerHTML	= '<div class="msg-error">Помилка: Некоректний місяць початку роботи</div>';
					$('work_success').style.display	= 'none';
					$('work_error').style.display	= 'none';
				}
				else
				{
					document.work_form.submit();
				}
			}
			
			if (($('work_history_'+i+'_end_year_select').value == -1) && ($('work_history_'+i+'_end_month_select').value == -1))
			{
				document.work_form.submit();
			} else {
			
			if((($('work_history_'+i+'_end_year_select').value>0) && ($('work_history_'+i+'_end_year_select').value>=$('work_history_'+i+'_start_year_select').value) ))
			{		
				document.work_form.submit();
			}
			else
			{
				$('work_history_result').innerHTML	= '<div class="msg-error">Помилка: Некоректний рік закінчення роботи</div>';
				$('work_success').style.display	= 'none';
				$('work_error').style.display	= 'none';
			}
			
			}
		}
	}
	else
	{
		document.work_form.submit();
	}
}
   